//
//  HGMusicPlayCollectionViewCell.m
//  悦享
//
//  Created by dllo on 16/3/22.
//  Copyright © 2016年 张洪国. All rights reserved.
//

#import "HGMusicPlayCollectionViewCell.h"
#import "SongModel.h"
#import <UIImageView+WebCache.h>
@implementation HGMusicPlayCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
        
        //生成通知让图片旋转或停止
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startOrStopAnimation:) name:@"imageAnimation" object:nil];
    }
    return self;
}

- (void)startOrStopAnimation:(NSNotification *)notification{
    if ([notification.object isEqualToString:@"isPlay"]) {
       
        _imageView1.layer.speed = 0;
        
    }else{
        [self startAnimation];
    }
    
    
}

- (void)startAnimation{
   //CAlAYER动画
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //旋转的起始值
    animation.fromValue = [NSNumber numberWithFloat:0];
    //旋转的结束值
    //π是180 2π360
    animation.toValue = [NSNumber numberWithFloat:M_2_PI];
    //重复次数
    animation.repeatCount = NSIntegerMax;
    //时间
    animation.duration = 3;
    //是否自动回到原来位置
//    animation.autoreverses = YES;
    //是否按照结束位置继续旋转
    animation.cumulative = YES;
    
    [_imageView1.layer addAnimation:animation forKey:@"标记"];
    
    //图片旋转
    //    self.imgView.transform = CGAffineTransformRotate(self.imgView.transform, M_PI/180);
    // 停止旋转
    //    self.imgView.transform = CGAffineTransformIdentity;
    
}
- (void)createSubViews{
    
    self.imageView1 = [[UIImageView alloc] init];
    [self.contentView addSubview:_imageView1];
    
    self.nameLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_nameLabel];
    
    
    self.singerLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_singerLabel];
    
    self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_playButton];
    
    self.minuButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_minuButton];
    
}

- (void)setModel:(SongModel *)model{
    _model = model;
    _nameLabel.text = _model.name;
    _singerLabel.text = _model.singerName;
    
    [_imageView1 sd_setImageWithURL:[NSURL URLWithString:_model.picUrl] placeholderImage:[UIImage imageNamed:PLACE_HLODER]];
   
    //标记程序刚运行的时候不让其旋转
    if (_model.isRow0 == YES) {
        
    }else{
        
        [self startAnimation];
    }
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    _imageView1.frame = CGRectMake(8, 1, 46, 46);
    _imageView1.layer.cornerRadius = 23;
    _imageView1.layer.masksToBounds = YES;
    _imageView1.backgroundColor = [UIColor redColor];
    _imageView1.layer.borderColor = [UIColor cyanColor].CGColor;
    _imageView1.layer.borderWidth = 1;
    
    _nameLabel.frame = CGRectMake(75, 5, 150, 20);
//    _nameLabel.backgroundColor = [UIColor cyanColor];
    _nameLabel.textColor = [UIColor blackColor];
    _nameLabel.font = [UIFont systemFontOfSize:15];
    
    _singerLabel.frame = CGRectMake(75, 27, 100, 20);
//    _singerLabel.backgroundColor = [UIColor redColor];
    _singerLabel.font = [UIFont systemFontOfSize:13];
    _singerLabel.textColor = [UIColor lightGrayColor];
}

@end
